var app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		id0: '0',
		id1: '1',
		id2: '2',
		id3: '3',
		id4: '4',
		id5: '5',
		id6: '6',
		id7: '7',
		id8: '8',
		id9: '9',
		id10: '←',
		id11: 'AC',

		formula: '',
		screenData: '',
		subject: [],

		pause: false,  //控制用户连续点击

		record: [],  //最终答题统计后的题目集合
		counterID: '',  //所查询的元素的ID

		timer: '',  //定时器名字
		timeleft: 0,//显示的剩余时间
		countDownNum: 60,//倒计时初始值
		flag: true,  //是否有计时器

		i: 1,
		sum: 30,
		right: 0,
		wrong: 0,

		judge: 3  //1代表正确，2代表错误，3代表还未提交答案
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

		this.setData({ i: 1 });
		this.setData({ judge: 3 });
		this.setData({ record: [] });   //提交结果记录
		this.setData({ flag: true });   //计时器
		this.setData({ pause: false }); //连续点击
		this.setData({ right: 0 });
		this.setData({ wrong: 0 });
		this.setData({ countDownNum: 20 });
		

		//调函数获得大数组
		this.setData({ subject: app.globalData.wrong_questions });
		this.setData({ sum: this.data.subject.length });
		// console.log(this.data.subject);
		this.setData({ formula: this.data.subject[this.data.i - 1][0] });
		this.countDown();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
		if (this.data.flag == true)//防止用户点左上角返回
			clearInterval(this.data.timer);
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},

	clickButton: function (event) {
		var id = event.target.id;
		if (id == this.data.id10) { //退格
			var data = this.data.screenData;
			if (data == "") {
				return;
			}
			data = data.substring(0, data.length - 1);
			this.setData({ screenData: data });
		}
		else if (id == this.data.id11) {//清屏
			this.setData({ screenData: "" });
		}
		else {
			var sd = this.data.screenData;
			if (sd.length <= 5) {
				var data;
				if (sd == "0") {
					data = id;
				}
				else {
					data = sd + id;
				}
				this.setData({ screenData: data });
			}
		}
	},

	countDown: function () {
		var countDownNum = this.data.countDownNum;//获取倒计时初始值
		this.setData({ timeleft: countDownNum });
		var that = this;
		//如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
		that.data.timer = setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
			if (countDownNum == 0) {
				//计时器是始终一直在走的，如果时间为0，那么就要关掉定时器,不然耗性能
				that.confirm();
			}
			else {
				countDownNum--;
				that.setData({ timeleft: countDownNum });
			}
		}, 1000)
	},

	confirm: function () {
		if (this.data.pause == false) {
			this.setData({ pause: true });
			if (this.data.flag == true)
				clearInterval(this.data.timer);
			//判断screendata与答案是否相等
			//根据情况修改jugde的值
			if (this.data.screenData == String(this.data.subject[this.data.i - 1][1])) {
				var right = this.data.right + 1;
				var list = [];
				this.setData({ right: right });
				this.setData({ judge: 1 });
				var ID = '';
				const db = wx.cloud.database();
				db.collection('history').get().then(res => {
					this.setData({
						counterID: this.getID(res.data, this.data.subject[this.data.i-1][0]),
					})
					db.collection('history').doc(String(this.data.counterID)).remove({
						success: res => {
							this.setData({
								counterID: '',
							})
						},
						fail: err => {
							console.error('[数据库] [删除记录] 失败：', err)
						}
					})
				})		
			}
			else {
				var wrong = this.data.wrong + 1;
				this.setData({ wrong: wrong });
				this.setData({ judge: 2 });
			}
			//记录对错，正确答案，formula+screenData存到record去
			var record = [];
			var str = this.data.formula;
			str = str.replace(/\[/g, '[ ');
			str = str.replace(/\]/g, this.data.screenData + ' ]');
			record.push(str);
			record.push(this.data.judge);
			record.push(this.data.subject[this.data.i - 1][1]);
			this.data.record.push(record);

			var that = this;
			setTimeout(function () {
				var i = that.data.i + 1;
				if (i == that.data.sum + 1) {//跳到结束页面去
					wx.setStorageSync('right', that.data.right);
					wx.setStorageSync('wrong', that.data.wrong);
					wx.setStorageSync('record', that.data.record);
					wx.redirectTo({
						url: '../result/result',
					})
				}
				else {
					that.setData({ i: i });
					that.setData({ judge: 3 });
					that.setData({ screenData: "" });
					if (that.data.flag == true)
						that.countDown();  //重新设置计时器计时
					//重新设置计算数的值
					that.setData({ formula: that.data.subject[that.data.i - 1][0] });
					that.setData({ pause: false });
				}
			}, 1000)
		}
	},

	getID: function(list,  formula) {  //在list中查询公式为formul的记录
		for (var i in list) {
			if (list[i].formula == formula) {
				// console.log("--------");
				// console.log(list[i]._id);
				// console.log("--------");
				return list[i]._id;
			}
		}
	}
})